<?php
include('../../includes/classes/core.php');
$start  	= $_REQUEST['start'];
$end    	= $_REQUEST['end'];
$count 		= $_REQUEST["count"];
$action 	= $_REQUEST['act'];
$departament= $_REQUEST['departament'];
$type       = $_REQUEST['type'];
$category   = $_REQUEST['production_category_id'];
$s_category = $_REQUEST['sub_category'];
$done 		= $_REQUEST['done']%3;
$name 		= $_REQUEST['name'];
$title 		= $_REQUEST['title'];
$text[0] 	= "ინფორმაციის წყაროს მიხედვით";
$text[1] 	= "'$category' ინფორმაციის წყაროს მიხედვით";
$text[2] 	= "'$type'- შემოსული  ზარები პროდუქტების მიხედვით";
$text[3] 	= "'$departament'- შემოსული  ქვე–კატეგორიის მიხედვით";
//------------------------------------------------query-------------------------------------------
switch ($done){
	case  1:
		$result = mysql_query("	SELECT info_category.`name`,
											COUNT(*),
											CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
												FROM 	incomming_call
												JOIN 	info_category ON info_category.id=incomming_call.information_sub_category_id
												JOIN  surce ON surce.id=incomming_call.source_id
								 				WHERE (surce.`name`='$category')
												AND incomming_call.actived=1
												AND DATE(`incomming_call`.`date`) >= '$start' 
												AND  DATE(`incomming_call`.`date`) <= '$end'  
												)*100,2),'%')
									FROM 	incomming_call
									JOIN 	info_category ON info_category.id=incomming_call.information_sub_category_id
									JOIN  surce ON surce.id=incomming_call.source_id
								 	WHERE (surce.`name`='$category')
									AND incomming_call.actived=1
									AND DATE(`incomming_call`.`date`) >= '$start' 
									AND  DATE(`incomming_call`.`date`) <= '$end' 
								 	GROUP BY info_category.`name`");
		$text[0]=$text[1];
	break;
	 case 2:
	$result = mysql_query("SELECT 	info_category.`name` as c_name,
										COUNT(*),
										CONCAT(ROUND(
										COUNT(*)/(SELECT COUNT(*)
											FROM incomming_call
											JOIN  surce ON surce.id=incomming_call.source_id
											JOIN info_category AS inf1 ON inf1.`name`='$type'
											JOIN info_category ON info_category.id=incomming_call.information_category_id AND info_category.parent_id=inf1.id
											WHERE (surce.`name`='$category')
											AND incomming_call.actived=1
											AND DATE(`incomming_call`.`date`) >= '$start' 
											AND DATE(`incomming_call`.`date`) <= '$end' 
											)*100,2),'%')
								FROM 	incomming_call
								JOIN 	info_category AS inf1 ON inf1.`name`='$type'
								JOIN  	surce ON surce.id=incomming_call.source_id
								JOIN 	info_category ON info_category.id=incomming_call.information_category_id AND info_category.parent_id=inf1.id
								WHERE (surce.`name`='$category') 
								AND incomming_call.actived=1
								AND DATE(`incomming_call`.`date`) >= '$start' 
								AND DATE(`incomming_call`.`date`) <= '$end' 
								GROUP BY c_name");
		$text[0]=$text[2];
	break;
	/*case 3:
		$result = mysql_query("SELECT 	info_category.`name` as c_name,
										COUNT(*),
										CONCAT(ROUND(
										COUNT(*)/(SELECT COUNT(*)
											FROM incomming_call
											JOIN info_category AS inf1 ON inf1.`name`='$category'
											JOIN info_category ON info_category.id=incomming_call.information_sub_category_id AND info_category.parent_id=inf1.id
											JOIN department ON incomming_call.department_id=department.id
											WHERE (incomming_call.call_type_id=$c) and DATE(`incomming_call`.`date`) >= '$start' AND DATE(`incomming_call`.`date`) <= '$end' AND department.`name`='$departament'
											)*100,2),'%')
								FROM 	incomming_call
								JOIN 	info_category AS inf1 ON inf1.`name`='$category'
								JOIN 	info_category ON info_category.id=incomming_call.information_sub_category_id AND info_category.parent_id=inf1.id
								JOIN 	department ON incomming_call.department_id=department.id
								WHERE (incomming_call.call_type_id=$c) and DATE(`incomming_call`.`date`) >= '$start' AND DATE(`incomming_call`.`date`) <= '$end' AND department.`name`='$departament'
								GROUP BY c_name");
		$text[0]=$text[3];
		break; */
	default:
		$result = mysql_query("SELECT CASE
										WHEN incomming_call.source_id=1 THEN'ტელეფონი'
										WHEN incomming_call.source_id=2 THEN'ჩათი'
										WHEN incomming_call.source_id=3 THEN'facebook'
										WHEN incomming_call.source_id=5 THEN'ელ-ფოსტა'
										END as type,
										COUNT(*),
										CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM incomming_call
																WHERE incomming_call.actived=1 
																AND DATE(`incomming_call`.`date`) >= '$start' 
																AND DATE(`incomming_call`.`date`) <= '$end')*100,2),'%')
								FROM 	incomming_call
								WHERE incomming_call.actived=1  
								AND	DATE(`incomming_call`.`date`) >= '$start' 
								AND  DATE(`incomming_call`.`date`) <= '$end'
								AND incomming_call.source_id!=0
								GROUP BY 	type");

		break;
}
///----------------------------------------------act------------------------------------------
switch ($action) {
	case "get_list":
		$data = array("aaData"	=> array());
		while ( $aRow = mysql_fetch_array( $result ) )
		{	$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[0] = '0';

				$row[$i+1] = $aRow[$i];
			}
			$data['aaData'][] =$row;
		}
		echo json_encode($data); return 0;
		break;
	case 'get_category' :
		$rows = array();
	//	print_r( $result);
		while($r = mysql_fetch_array($result)) {
			$row[0] = $r[0];
			$row[1] = (float) $r[1];
			$rows['data'][]=$row;
		}
		$rows['text']=$text[0];
		echo json_encode($rows);
		break;
	default :
		echo "Action Is Null!";
		break;

}



?>